package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.*;
import com.mall.db.service.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/ct")
public class CommodityController extends BaseController<Commodity> {
    @Resource(name = "commodityService")
    private CommodityService cs;
    @Resource(name = "itemCatService")
    private ItemCatService is;
    @Resource(name = "briefService")
    private BriefService bs;
    @Resource(name = "stockService")
    private StockService ss;
    @Resource(name = "parameterService")
    private ParameterService pc;

    @Override
    public Map<String, Object> find(Commodity commodity) {
        PageHelper.startPage(commodity.getPage(), commodity.getLimit());
        QueryWrapper qw = new QueryWrapper();
        List<Commodity> list = cs.list(qw);
        PageInfo<Commodity> pageInfo = new PageInfo<>(list, commodity.getLimit());
        return findMap(pageInfo.getList(), pageInfo.getTotal());

    }

    @Override
    public Map<String, Object> delete(Commodity commodity) {
        boolean b = cs.removeById(commodity.getId());
        return this.iud("删除", b, commodity);
    }

    @RequestMapping("/upState")
    public Map<String, Object> updateState(Commodity commodity) {
        boolean b = cs.updateById(commodity);
        return this.iud(commodity.getState() + "", b, commodity);
    }

    @RequestMapping("/findCommodity")
    public Map<String, Object> findCommodity(@RequestParam(name = "ids") List<Integer> ids) {
        QueryWrapper<Commodity> qw = new QueryWrapper<>();
        qw.in("id", ids);
        List<Commodity> list = cs.list(qw);
        return findMap(list, 10);
    }
    @RequestMapping("/up")
    public Map<String,Object> up(Commodity commodity){
        QueryWrapper qw=new QueryWrapper();

        ItemCat itemCat1=new ItemCat();
        itemCat1.setName(commodity.getName());
        itemCat1.setKeyWord(commodity.getKeyword());
        itemCat1.setIntroduction(commodity.getIntroduction());
        itemCat1.setPid(commodity.getFenLei());
        itemCat1.setCreateTime(new Date());
        is.save(itemCat1);

        List<Brief> list=bs.list(qw);

        if(list.size()!=0){
            for (Brief br:list) {
                String name=br.getBeName();
                String value=br.getBeValue();
                commodity.setSpecifications(name+" : "+value);
            }
        }else {
            commodity.setSpecifications("规格 : 标准");
        }

        Stock s=ss.getById(1);

        if(!StringUtils.isEmpty(s)){
            commodity.setStock(s.getNumber());
        }else {
            commodity.setStock(0);
        }

        List<Parameter> list1=pc.list(qw);

        if(list1.size()!=0){
            for (Parameter p:list1) {
                String name=p.getParameterName();
                String value=p.getParameterValue();
                commodity.setParameters(name+" : "+value);
            }
        }
        boolean b=cs.save(commodity);
        bs.remove(qw);
        pc.remove(qw);
        ss.remove(qw);
        Stock s1=new Stock();
        s1.setCation("标准");
        s1.setPrice("0");
        s1.setNumber(0);
        s1.setState(1);
        ss.save(s1);
        return this.iud("上架",b,commodity);
    }
}
